var static={
	area:[
			['1','华北'],
			['2','东北'],
			['3','华东'],
			['4','中南'],
			['5','西南'],
			['6','西北'],
			['7','港奥台'],
			['8','其他']
		 ],
	provinces: 
		[
			//['-1',['010','北京','BEIJING']],
			//['-1',['020','上海','SHANGHAI']],
			//['-1',['030','天津','TIANJIN']],
			//['-1',['040','重庆','CHONGQING']],
			['4',['050','广东省','GUANGDONG'],['110','广西自治区','GUANGXI'],['130','海南省','HAINAN'],['150','河南省','HENAN'],['170','湖北省','HUBEI'],['180','湖南省','HUNAN']],
			['3',['060','江苏省','JIANGSU'],['070','浙江省','ZHEJIANG'],['080','安徽省','ANHUI'],['090','福建省','FUJIAN']],
			['6',['100','甘肃省','GANSU']],
			['5',['120','贵州省','GUIZHOU']],
			['1',['140','河北省','HEBEI']],
			['2',['160','黑龙江省','HEILONGJIANG'],['190','吉林省','JILIN']]
	    ],
	city:
		[
			['050',['050020','广州','GUANGZHOU'],['050030','潮州','CHAOZHOU'],
			       ['050040','东莞','DONGGUAN'],['050050','佛山','FOSHAN'],
			       ['050060','惠州','HUIZHOU'],['050070','清远','QINGYUAN'],
			       ['050080','汕头','SHANTOU'],['050090','深圳','SHENZHEN'],
			       ['050100','顺德','SHUNDE'],['050110','湛江','ZHANJIANG'],
			       ['050120','肇庆','ZHAOQING'],['050130','中山','ZHONGSHAN'],
			       ['050140','珠海','ZHUHAI']],
			['060',['060020','南京','NANJIN'],['060030','常熟','CHANGSHU'],
			       ['060040','常州','CHANGZHOU'],['060050','昆山','KUNSHAN'],
		           ['060060','连云港','LIANYUNGANG'],['060070','南通','NANTONG'],
			       ['060080','苏州','SUZHOU'],['060090','太仓','TAICANG'],
		           ['060100','无锡','WUXI'],['060110','徐州','XUZHOU'],
			       ['060120','扬州','YANGZHOU'],['060130','镇江','ZHENJIANG']],
			['070',['070020','杭州','HANGZHOU'],['070030','宁波','NINGBO'],
				   ['070040','温州','WENZHOU'],['070050','绍兴','SHAOXING'],
				   ['070060','金华','JINHUA'],['070070','台州','TAIZHOU'],
				   ['070080','湖州','HUZHOU'],['070090','嘉兴','JIAXING'],
				   ['070100','衢州','QUZHOU'],['070110','丽水','JIAXING'],
				   ['070120','舟山','ZHOUSHAN']],
			['080',['080020','合肥','HEFEI'],['080030','安庆','ANQING'],
				   ['080040','蚌埠','BENGBU'],['080050','芜湖','WUHU']],
			['090',['090020','福州','FUZHOU'],['090030','泉州','QUANZHOU'],
				   ['090040','厦门','XIAMEN'],['090050','漳州','ZHANGZHOU']],
			['100',['100020','兰州','LANZHOU'],['100030','嘉峪关','JIAYUGUAN'],
				   ['100040','酒泉','JIUQUAN']],
			['110',['110020','南宁','NANNING'],['110030','北海','BEIHAI'],
				   ['110040','桂林','GUILIN'],['110050','柳州','LIUZHOU'],
				   ['110060','玉林','YULIN']],
			['120',['120020','贵阳','GUIYANG'],['120030','遵义','ZUNYI']],
			['130',['130020','海口','HAIKOU'],['130030','三亚','SANYA']],
			['140',['140020','石家庄','SHIJIAZHUANG'],['140030','保定','BAODING'],
				   ['140040','承德','CHENGDE'],['140050','邯郸','HANDAN'],
				   ['140060','廊坊','LANGFANG'],['140070','秦皇岛','QINHUANGDAO'],
				   
				   ['140080','唐山','TANGSHAN'],['140090','张家口','ZHANGJIAKOU']],
			['150',['150020','郑州','ZHENGZHOU'],['140030','开封','KAIFENG'],
				   ['150040','洛阳','LUOYANG']],
			['160',['160020','哈尔滨','HAERBING'],['160030','大庆','DAQING'],
				   ['160040','佳木斯','JIAMUSI'],['160050','牡丹江','MUDANJIANG'],
				   ['160060','齐齐哈尔','QIQIHAER']],
			['170',['170020','武汉','WUHAN'],['170030','十堰','SHIYAN'],
				   ['170040','襄樊','XIANGFAN'],['170050','宜昌','YICHANG'],
				   ['170060','潜江','QIANJIANG'],['170070','荆门','JINGMEN'],
				   ['170080','荆州','JINGZHOU'],['170090','黄石','HUANGSHI']],
			['180',['180020','长沙','CHANGSHA'],['180030','湘潭','XIANGTAN'],
				   ['180040','株洲','ZHUZHOU'],['180050','常德','CHANGDE'],
				   ['180060','衡阳','HENGYANG'],['180070','益阳','YIYANG'],
				   ['180080','郴州','CHENZHOU'],['180090','岳阳','YUEYANG']],
			['190',['190020','长春','CHANGCHUN'],['190030','吉林市','JINLINSHI']]

					//从这里看是添加 其他不要动
		],
	tradecategory:
		[
			['010','IT|互联网|通信|电子'],
			['020','金融|银行|保险']

		],
	trade:
		[
		 
		 	['010',['010010','计算机软件'],['010020','IT服务(系统/数据/维护)/多领域经营'],
				   ['010030','计算机硬件及网络设备'],
				   ['010040','互联网/电子商务'],['010050','网络游戏'],['010060','通信/电信/网络设备'],
				   ['010070','电子技术/半导体/集成电路务']],
			['020',['020010','金融/投资/基金/证券/期货'],['020030','银行'],['020040','保险'],
				   ['020040','房地产|建筑建设|物业']]
		],
	jobcategory:
		[
		 	['010','销售类'],
			//['020','经营管理类'],
			['030','计算机·网络·技术类']
		],
	job:[
			['010',['010010','区域销售总监'],['010020','销售总监'],['010030','销售经理·销售主管'],['010040','区域销售经理'],
				   ['010050','渠道总监·分销总监'],['010060','渠道分销经理·渠道分销主管'],['010070','售前支持经理·售前支持主管'],
				   ['010080','售前支持工程师'],['010090','商务经理·商务主管'],['010100','业务拓展(BD)经理'],['010110','其他销售类']],
			['030',['030010','计算机软件'],['030020','IT服务(系1统/数据/维护)/多领域经营'],
				   ['030030','计算机硬件及网络设备'],
				   ['030040','互联网/电子商务'],['0300050','网络游戏'],['030060','通信/电信/网络设备'],
				   ['030080','计算机软件'],['030090','IT服务(系统/数据/维护)/多领域经营']]
		]
};
$(function(){
	
	function Selinput(){
		this.config=Selinput._default;
		this.templates=Selinput._templates;
	};
	Selinput.prototype={
		mergeObject:function(_obj){
			var obj={};
            for(var attr in this.config){  
                obj[attr]=_obj[attr]||this.config[attr];  
            }
			switch(_obj["name"]){
				case "trade":
					obj["data"]=[static.tradecategory,static.trade];
					break;
				case "area":
					obj["data"]=[static.area,static.provinces,static.city];
					break;
				case "job":
					obj["data"]=[static.jobcategory,static.job];
					break;
			}
			return obj;
        },
		get_area_index:function(code){
			var arr=this.config.data[1];
			for(var i in arr){
				for(var k=1;k<arr[i].length;k++){
					if(arr[i][k][0]==code){
						return arr[i][0]-1;
						break;
					}
				}
			}
		},
		bindArea:function(obj){
			this.top=$(this.templates.areashow);
			this.city_show=0;
			this.result=this.top.find(".province");
			this._temp_result=this.top.find(".city");
			var value=obj.val();
			if(value!=""){
				obj.temp=[];
				var index=(value.indexOf(",")>-1)?value.indexOf(","):value.length;
					_content=value.substring(0,index),
				temp_data=value.split(",");
				if(_content.length==3){
					obj.temp.push(this.get_area_index(_content),temp_data);
					this._temp_result.hide();
				}
				if(_content.length==6){
					obj.temp.push(this.get_area_index(_content.substring(0,3)),_content.substring(0,3),temp_data);
					this._temp_result.show();
					this.result=this.top.find("ul.city");
					this.top.find(".tab_show").html("隐藏城市");
					
				}
			}else{
				obj.temp=[];
				obj.temp.push(0,[0]);
				this._temp_result.hide();
			}
			this.area_index=obj.temp[0];		  //地区索引
			this.bindAreaRoot(obj);
			
		
		},
		bindAreaRoot:function(obj){
			var arr=this.config.data[0],
				_temp=[],
				target=this.top.find("ul.area"),
				_this=this;
			for(var i in arr){
				_li=this.templates.parentli;
				_li=_li.replace(/#id#/,arr[i][0]);
				_li=_li.replace(/#name#/,arr[i][1]);
				_temp.push(_li);
			}
			var _temp_li=$(_temp.join(""));
			target.append(_temp_li);
			
			_temp_li.eq(this.area_index).addClass("current");
			_temp_li.bind("click",function(){
				_temp_li.get(_this.area_index).className="";
				$(this).addClass("current");
				_this.area_index=_temp_li.index($(this));
				var code=$(this).attr("code");
				if(!_this._temp_result.is(":hidden")){
					_this._temp_result.show();
				}
				_this.bindAreaparent(code,obj);
			})
			this.bindAreaparent(arr[this.area_index][0],obj);
		},
		bindAreaparent:function(code,obj){
			var arr=this.config.data[1],
				_temp='',
				_this=this,
				tab_show=this.top.find(".tab_show");
			tab_show.unbind( "click" );
			tab_show.bind("click",function(){
				if(_this._temp_result.css("display")=="none"){
					_this._temp_result.show();
					$(this).html("隐藏城市");
					_this.result=_this.top.find("ul.city");
					_this.city_show=1;
				}else{
					_this._temp_result.hide();
					_this.result=_this.top.find("ul.province");
					$(this).html("显示城市");
					_this.city_show=0;
				}
				_this.init_provnce_html(code,obj);
			});
			if(obj.temp.length===3&&this.city_show==1){
				_this._temp_result.show();
			}
			_this.init_provnce_html(code,obj);
		},
		init_provnce_html:function(code,obj){ //初始化省html
			var arr=this.config.data[1],
				_this=this,
				count=0,
				target=this.top.find("ul.province").empty();
			for(var i in arr){
				if(arr[i][0]==code){
					var _arr=[],
						_li="",
						_temp=arr[i][1][0];
					for(var k=1;k<arr[i].length;k++){
						//checkbox的切换
						if(_this._temp_result.css("display")=="none"){
							_li=this.templates.childli;
							_li=_li.replace(/#type#/,this.config.seltype);
							for(var j in obj.temp[1]){
								if(arr[i][k][0]==obj.temp[1][j]){
									_li=_li.replace(/#checked#/,"checked");
									count++;
								}
							}
						}else{
							_li=this.templates.parentli;
							if(this.city_show==0){
								_temp=obj.temp[1];
								this.city_show=1;
							}
							if(arr[i][k][0]==obj.temp[1]){
								_li=_li.replace(/#class#/,'class="current"');
							}
						}
						_li=_li.replace(/#id#/,arr[i][k][0]);
						_li=_li.replace(/#name#/,arr[i][k][1]);	
						_arr.push(_li);
					}
					target.append($(_arr.join("")));
					break;
				}
			}
			if(count==_this.config.maxlength){
				target.find("input:not(:checked)").attr("disabled","true");
			}
			target.find("li").bind("click",function(){
				if(_this._temp_result.is(":hidden")){
					var len=$(this).parents("ul").find("input:checked").length;
					if(len==_this.config.maxlength){
						$(this).parents("ul").find("input:not(:checked)").attr("disabled","true");
					}else{
						$(this).parents("ul").find("input:not(:checked)").removeAttr("disabled");
					}
					return;
				}
				var code=$(this).attr("code");
				_this.bindAreachid(code,obj);
			})
			this.bindAreachid(_temp,obj);
		},
		bindAreachid:function(code,obj){
			var arr=this.config.data[2],
				count=0,
				_temp=[],
				_this=this,
				target=this.top.find("ul.city").empty();
			for(var i in arr){
				if(arr[i][0]==code){
					var arr_select=[];
					for(var k=1;k<arr[i].length;k++){
						_li=this.templates.childli;
						_li=_li.replace(/#id#/,arr[i][k][0]);
						_li=_li.replace(/#name#/,arr[i][k][1]);
						_li=_li.replace(/#type#/,this.config.seltype);
						for(var j in obj.temp[2]){
							if(arr[i][k][0]==obj.temp[2][j]){
								_li=_li.replace(/#checked#/,"checked");
								count++;
							}
						}
						arr_select.push(_li);
					}
					target.append($(arr_select.join("")));
					break;
				}
			}//加事件
			if(count==_this.config.maxlength){
				target.find("input:not(:checked)").attr("disabled","true");
			}
			target.bind("click",function(){
				var len=target.find("input:checked").length;
				if(len==_this.config.maxlength){
					$(this).find("input:not(:checked)").attr("disabled","true");
				}else{
					$(this).find("input:not(:checked)").removeAttr("disabled");
				}
			});
		},
		getparentindex:function(code){
			for(var i in this.config.data[0]){
				if(this.config.data[0][i][0]==code){
					return i;
				}
			}
		},
		bindParentlist:function(obj){
		
			if(this.config.name=="area"){
				this.bindArea(obj);
				return;
			}
			if(obj.val()!=""){
				obj.temp=[];
				var code=this.getparentindex(obj.val().substring(0,3));
				obj.temp.push(code,obj.val().split(","));
			}
			var index=typeof obj.temp=="undefined"?0:obj.temp[0];
			this.parent_index=index;
			this.top=$(this.templates.show);
			this.result=this.top.find(".cols:eq(1)");
			var	result = this.top.find(".selector_title"),
				p_List = this.top.find(".cols:first").find("ul"),
				cur_array=this.config.data[0],
				_temp=[],_li="";
			for(var i in cur_array){
				_li=this.templates.parentli;
				_li=_li.replace(/#id#/,cur_array[i][0]);
				_li=_li.replace(/#name#/,cur_array[i][1]);
				_temp.push(_li);
			}
			var _temp_p=$(_temp.join(""));
				_this=this;
			p_List.append(_temp_p);
			_temp_p.eq(this.parent_index).addClass("current");
			_temp_p.bind("click",function(){
				_temp_p.get(_this.parent_index).className="";
				$(this).addClass("current");
				_this.parent_index=_temp_p.index($(this));
				_this.bindChildlist($(this).attr("code"),obj);
				
			});
			_this.bindChildlist(this.config.data[1][index][0],obj);
		},
		getData:function(){
			var _input=this.result.find("input"),
			 	 result=[];
			_input.each(function(){
					if($(this).is(":checked")){
			//var _temp=[$(this).val(),$(this).get(0).nextSibling.nodeValue];
						var _temp=$(this).val();
						result.push(_temp);
					}
				}
			 )
			return result;
		},
		//绑定list
		bindChildlist:function(code,obj){
			var cur_array=this.config.data[1],
				target=this.result.find("ul").empty(),
				_temp=[];
			for(var i in cur_array){
				if(cur_array[i][0]==code){
					var arr_select=[];
					for(var k=1;k<cur_array[i].length;k++){
						_list=this.templates.childli;
						_list=_list.replace(/#id#/,cur_array[i][k][0]);
						_list=_list.replace(/#name#/,cur_array[i][k][1]);
						_list=_list.replace(/#type#/,this.config.seltype);
						if(typeof obj.temp!="undefined"&&obj.temp[1]){
							 for(var l in obj.temp[1]){
								if(cur_array[i][k][0]==obj.temp[1][l]){
									_list=_list.replace(/#checked#/,"checked");
								}
							 }
						}
						_temp.push(_list);
					}
					target.append($(_temp.join("")));
					break;
				}
			}
			if(typeof obj.temp!="undefined"&&obj.temp[1]&&obj.temp[0]==this.parent_index){
				if(obj.temp[1].length>=this.config.maxlength){
					target.find("input:not(:checked)").attr("disabled","true");
				}else{
					$(this).find("input:not(:checked)").removeAttr("disabled");
				}
			}
			target.bind("click",function(){
				var len=target.find("input:checked").length;
				if(len>=_this.config.maxlength){
					$(this).find("input:not(:checked)").attr("disabled","true");
				}else{
					$(this).find("input:not(:checked)").removeAttr("disabled");
				}
			});
		},
		//获取父name
		getParent_name:function(code){
			var index=0;
			if(this.config.name=="area"){
				index=1;
			}
			var cur_array=this.config.data[index];
			for(var i in cur_array){
				if(cur_array[i][0]==code){
					return cur_array[i][1];
				}else{
					for(var j=1;j<cur_array[i].length;j++){
						if(cur_array[i][j][0]==code){
							return cur_array[i][j][1];///英语的时候要改
						}
					}
				}
			}
		},
		//获取字name
		getChild_name:function(code){
			var _l=this.config.data.length;
				 cur_array=this.config.data[_l-1],
			    _temp=[];
			for(var i in cur_array){
				if(cur_array[i][0]==code.substring(0,3)){
					_temp=cur_array[i];
					break;
				}
			}
			for(var k=1;k<_temp.length;k++){
				if(_temp[k][0]==code){
					return _temp[k][1];
					break;
				}
			}
		},
		//显示
		showDlg:function(obj){
			var _input=obj,
				_this=this;
				_this.bindParentlist(obj);
			$.dialog({
				title:"请选择",
				lock:true,
				padding:"15px 15px 0px 15px",
				content:this.top.get(0),
				okValue:"确定",
				cancelValue:"取消",
				cancel:function(){
				},
				ok:function(){
					var sel_data=_this.getData();
					_input.val(sel_data.join(","));
					_this.edit_style(_input);
	
				}
			})
		},
		//编辑时候的样式
		edit_style:function(obj){
			if(obj.val()==""){
				return;
			}
			var list=obj.val().split(","),
				li='<li name="#result#">#name#<em></em></li>',
				_p= obj.parent(),
				result =_p.find(".pop_selection_result").empty().addClass("pop_selection_hide"),
				modify = _p.find(".pop_selection_modify").addClass("pop_selection_hide"),
				selection =_p.find(".pop_selection").addClass("pop_selection_hide");
			for(var i in list){
				var _li=li.replace(/#result#/,list[i]);
				if(list[0].length==3){
					var _p_name=this.getParent_name(list[i]);
				}if(list[0].length==6){
					var _p_name=this.getChild_name(list[i]);
				}
				_li=_li.replace(/#name#/,_p_name);
				$(_li).appendTo(result);
					
			}
			obj.temp=[];
			if(this.config.name=="area"){
				if(list[0].length==3){
					obj.temp.push(this.area_index,list)
				}
				if(list[0].length==6){
					obj.temp.push(this.area_index,list[0].substring(0,3),list)//正在构建
				}
			}else{
				obj.temp.push(this.parent_index,list);	
			}
			modify.removeClass("pop_selection_hide");
    		result.removeClass("pop_selection_hide");
			result.find("em").bind("click",function(){
				var index=result.find("em").index(this);
				list.splice(index,1);
				obj.val(list.join(","));
				obj.temp.push(this.parent_index,list);
				$(this).parents("li").remove();
				if(obj.val()==""){
					delete obj["temp"];
					modify.addClass("pop_selection_hide");
					result.addClass("pop_selection_hide");
					obj.parent().find(".pop_selection").removeClass("pop_selection_hide");
				}
				
			})
		}
	};
	Selinput._templates={
		select:'<span class="pop_selection input-text"><em class="icon_selection"><a href="javascript:;"></a></em></span>',
		modify:'<a class="pop_selection_modify" href="javascript:;">修改</a>',
		result:'<ul class="pop_selection_result input-text pop_selection_inline_block"></ul>',
		parentli:'<li code="#id#" #class#><a href="javascript:;">#name#</a></li>',
		childli:'<li><label><input class="inall" #checked# type="#type#" name="scjobs" value="#id#"/>#name#</label></li>',
		show:'<div class="pop_cols_selector job-cols-list">'
+	      '<div class="selector_title">'
+	           '<strong></strong>'
+	           '<ul></ul>'
+	      '</div>'
+	      '<div class="col-list clearfix">'
+	          '<div class="cols"><h2>一级分类</h2><ul></ul></div>'
+	          '<div class="cols"><h2>二级分类</h2><ul></ul></div>'
+	     '</div>',
		areashow:'<div id="sw_area">'
+			'<ul class="area"></ul>'
+			'<ul class="province"></ul>'
+			'<ul class="city"></ul>'
+			'<div class="tab_show">显示城市</div></div>'
	};
	Selinput._default={
		"seltype":"checkbox",
		"all":true,
		"maxlength":2,
		"data":[],
		"name":"-1"   //trade行业  area地区  //工种  
	};
	(function(fyx){
		fyx.fn.bindselect=function(obj){
				var	_obj=new Selinput,
					select=_obj.templates.select,
				    modify=_obj.templates.modify,
				    result=_obj.templates.result;
					_obj.config=_obj.mergeObject(obj);
			this.each(function(){
				var _this=$(this), 
					_select=$(select).insertAfter(_this).width(_this.width()).bind("click",function(){_obj.showDlg(_this)}),
					_result=$(result).insertAfter(_select).addClass("pop_selection_hide"),
					_modify=$(modify).insertAfter(_result).addClass("pop_selection_hide").bind("click",function(){_obj.showDlg(_this)});
					_obj.edit_style(_this);////初始化样式*/
			});
			this.hide();
		}
	})(jQuery);
		   
});//15901121834